﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class PageIONew : Form
    {

        public PageIONew()
        {
            InitializeComponent();
        }
        bool IsFirst = true;
        public List<RadioButton> lst_radios_input = new List<RadioButton>();
        public List<RadioButton> lst_radios_output = new List<RadioButton>();
        private Image redLight;
        private Image greenLight;

        private void loadTable(DataGridView dataGrid,string strInput)
        {
            dataGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
            if (strInput.ToLower()=="input")
            {
                
                var arr_cardin1 = Enum.GetNames(typeof(e_input_card1));
                var arr_excardin1 = Enum.GetNames(typeof(e_input_extCard1));
                var arr_excardin2 = Enum.GetNames(typeof(e_input_extCard2));
                for (int i=0;i<56;i++)
                {
                    var str = "预留";
                    if (i < arr_cardin1.Length)
                        str = arr_cardin1[i];
                    else if (i < arr_excardin1.Length+8&&i>=8)
                        str = arr_excardin1[i-8];
                    else if (i < arr_excardin2.Length + 24 && i >= 24)
                        str = arr_excardin2[i - 24];
                    dataGrid.Rows.Add(new object[] { (i+1).ToString(), (i + 1).ToString() ,"●"
                    ,str });
                }
            }
            if (strInput.ToLower() == "output")
            {
                var arr_card1 = Enum.GetNames(typeof(e_output_card1));
                var arr_excard1 = Enum.GetNames(typeof(e_output_extCard1));
                var arr_excard2 = Enum.GetNames(typeof(e_output_extCard2));
                for (int i = 0; i < 56; i++)
                {
                    var str = "预留";
                    if (i < arr_card1.Length)
                        str = arr_card1[i];
                    else if (i < arr_excard1.Length + 8 && i >= 8)
                        str = arr_excard1[i - 8];
                    else if (i < arr_excard2.Length + 24 && i >= 24)
                        str = arr_excard2[i - 24];
                    dataGrid.Rows.Add(new object[] { (i+1).ToString(), (i + 1).ToString() ,
                       "●","置位","复位",str });
                  
                }
            }
        }
        private void PageIO_Load(object sender, EventArgs e)
        {
            if(IsFirst)
            {
                //redLight = Image.FromFile(Application.StartupPath + "\\UI\\Redlight.png");
                //greenLight = Image.FromFile(Application.StartupPath + "\\UI\\Greenlight.png");
                loadTable(dgv_output, "output");
                loadTable(dgv_input,"input");
                var redStype = Color.Red ;
                 var greenStype = Color.Green ;
              
                 Task.Factory.StartNew(new Action(() =>
                {
                    try
                    {
                        while (!GlobalVar.isExit)
                        {
                            Thread.Sleep(50);
                            Application.DoEvents();
                            this.BeginInvoke(new Action(() =>
                            {
                                for (int M = 0; M < 56; M++)
                                {
                                    var curCell= ((DataGridViewTextBoxCell)dgv_output.Rows[M].Cells[2]);
                                    curCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                                    
                                    if (M < GlobalMotion.Output_Card1.Length)
                                    {
                                        curCell.Style.ForeColor = GlobalMotion.Output_Card1[M] ? greenStype : redStype;
                                    }
                                    else if (M >= 8 && M < (GlobalMotion.Output_CardEx1.Length + 8))
                                    {
                                        curCell.Style.ForeColor = GlobalMotion.Output_CardEx1[M - 8] ? greenStype : redStype;
                                    }
                                    else if (M >= 24 && M < (GlobalMotion.Output_CardEx2.Length + 24))
                                    {
                                        curCell.Style.ForeColor = GlobalMotion.Output_CardEx2[M - 24] ? greenStype : redStype;
                                    }
     
                                }
                            }));
                        }
                    }
                    catch (Exception ex)
                    {
                        displalyInfo.displayErrorMain(ex.Message);
                    }
                }));
                Task.Factory.StartNew(new Action(() =>
                {
                    try
                    {
                        while (!GlobalVar.isExit)
                        {
                            Thread.Sleep(50);
                            this.BeginInvoke(new Action(() =>
                            {
                                for (int M = 0; M < 56; M++)
                                {
                                    var curCell = ((DataGridViewTextBoxCell)dgv_input.Rows[M].Cells[2]);
                                    curCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                                
                                    if (M < GlobalMotion.Input_Card1.Length)
                                    {
                                        curCell.Style.ForeColor = GlobalMotion.Input_Card1[M] ? greenStype :redStype ;
                                    }
                                    else if (M >= 8 && M < (GlobalMotion.Input_CardEx1.Length + 8))
                                    {
                                        curCell.Style.ForeColor = GlobalMotion.Input_CardEx1[M - 8] ? greenStype : redStype;
                                    }
                                    else if (M >= 24 && M < (GlobalMotion.Input_CardEx2.Length + 24))
                                    {
                                        curCell.Style.ForeColor = GlobalMotion.Input_CardEx2[M - 24] ? greenStype : redStype;
                                    }
                                }
                            }));
                        }
                    }
                    catch (Exception ex)
                    {
                        displalyInfo.displayErrorMain(ex.Message);
                    }
                }));
            }
        }

        private void dgv_output_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex>2&&e.RowIndex>=0)
            {
                var curCell=dgv_output.Rows[e.RowIndex].Cells[e.ColumnIndex];
                var curInf0 = curCell.Value;
                if(curInf0.ToString()=="复位")
                {
                    if (MotionControl.GlobalVariable.OperLevel != (int)MotionControl.operLevel.superAdmin && GlobalVar.isAutoRun)
                    {
                        MessageBox.Show("自动运行中  IO界面操作无效");
                        return;
                    }
                    if (e.RowIndex<8)
                    {
                        GlobalMotion.Card_1.setOut((ushort)e.RowIndex, MotionControl.e_out_OnOff.on);
                    }else if (e.RowIndex < 24)
                    {
                        GlobalMotion.CardExt_1.setOut((ushort)(e.RowIndex-8), MotionControl.e_out_OnOff.on);
                    }
                    else if (e.RowIndex < 56)
                    {
                        GlobalMotion.CardExt_2.setOut((ushort)(e.RowIndex - 24), MotionControl.e_out_OnOff.on);
                    }
                }
                else if (curInf0.ToString() == "置位")
                {
                    if (MotionControl.GlobalVariable.OperLevel != (int)MotionControl.operLevel.superAdmin && GlobalVar.isAutoRun)
                    {
                        MessageBox.Show("自动运行中  IO界面操作无效");
                        return;
                    }
                    if (e.RowIndex < 8)
                    {
                        GlobalMotion.Card_1.setOut((ushort)e.RowIndex, MotionControl.e_out_OnOff.off);
                    }
                    else if (e.RowIndex < 24)
                    {
                        GlobalMotion.CardExt_1.setOut((ushort)(e.RowIndex - 8), MotionControl.e_out_OnOff.off);
                    }
                    else if (e.RowIndex < 56)
                    {
                        GlobalMotion.CardExt_2.setOut((ushort)(e.RowIndex - 24), MotionControl.e_out_OnOff.off);
                    }
                }
            }
        }
    }
}
